package com.global.model;

import java.io.Serializable;
import javax.persistence.*;

import org.compass.annotations.SearchableProperty;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.hibernate.annotations.Type;

import com.global.comparator.GridComparator;

import java.util.List;

/**
 * The persistent class for the ZLY_ST_BSC_RPTX database table.
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate=true)
@Table(name="ZLY_ST_BSC_RPTX")
@SequenceGenerator(name="ZLY_ST_BSC_RPTX_ID_GENERATOR", sequenceName="ZLY_ST_BSC_RPTX_ID_SEQ", initialValue=100, allocationSize=1)
@NamedQueries({
        @NamedQuery(
                name = "findReportByName",
                query = "select r from Report r where upper(r.name) = :uniqueName "
        ),
        @NamedQuery(
                name = "findReportByReportCategory",
                query = "select r from Report r where r.category.id = :uniqueId "
        )      
})
public class Report extends BaseObject implements Serializable {

	private static final long serialVersionUID = 1500176775091721767L;
	
	private Long id;
	private String name;
	private String description;
	private int displayIndex;
	private boolean status;
	
	private List<Grid> gridList;
	private ReportCategory category;
	
    /**
     * 定义操作人、操作时间
     */
    private Who who;


    public Report() {
    }


	@Id	
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ZLY_ST_BSC_RPTX_ID_GENERATOR")
	public Long getId() {
		return this.id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * 获取报表的名称
	 * @return
	 */
	@Column(nullable=false, length=128, unique=true)
	@SearchableProperty
	public String getName() {
		return this.name;
	}
	/**
	 * 设置报表的名称
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 返回报表的有效状态
	 * @return boolean，true表示有效
	 */
	@Column(nullable=false, length=1)
	@Type(type="yes_no")
	public boolean getStatus() {
		return this.status;
	}
	/**
	 * 设置报表是否有效
	 * @param status
	 */
	public void setStatus(boolean status) {
		this.status = status;
	}

	/**
	 * 返回报表的描述
	 * @return
	 */
	@Column(length=1024, nullable=false)
	@SearchableProperty
	public String getDescription() {
		return this.description;
	}
	/**
	 * 设置报表的描述
	 * @param description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * 返回报表的显示顺序
	 * @return
	 */
	@Column(name="DISPLAY_INDEX", nullable=false)
	public int getDisplayIndex() {
		return this.displayIndex;
	}
	/**
	 * 设置报表的显示顺序
	 * @param displayIndex
	 */
	public void setDisplayIndex(int displayIndex) {
		this.displayIndex = displayIndex<0 ? 0 : displayIndex;
	}

	/**
	 * 返回报表的分类
	 * @return ReportCategory
	 */
	//bi-directional many-to-one association to SystemType
    @ManyToOne
	@JoinColumn(name="RPTXCATEGORY_ID", nullable=false)
	public ReportCategory getCategory() {
		return this.category;
	}
    /**
     * 设置报表的分类
     * @param reportCategory
     */
	public void setCategory(ReportCategory reportCategory) {
		this.category = reportCategory;
	}
	
	//bi-directional many-to-one association to Grid
	@OneToMany(mappedBy="report")
	@Sort(type=SortType.COMPARATOR, comparator=GridComparator.class)
	public List<Grid> getGridList() {
		return this.gridList;
	}
	public void setGridList(List<Grid> gridList) {
		this.gridList = gridList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Who getWho() {
		return who;
	}
	/**
     * {@inheritDoc}
     */
	@Override
	public void setWho(Who who) {
		this.who = who;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		
		Report other = (Report) obj;
		
		if (id != other.id){
			if (name == null) {
				if (other.name != null)
					return false;
			} else if (!name.equalsIgnoreCase(other.name)){
				return false;
			}
		}
		
		return true;
	}

	@Override
	public String toString() {
		return "Report [name=" + name + ", description=" + description
				+ ", status=" + status + "]";
	}	
}